package com.dykj.fireFighting.server.socialUnit.firemanger.service.impl;

import com.dykj.fireFighting.api.socialUnit.buildmanger.service.IPartitionsService;
import com.dykj.fireFighting.api.socialUnit.devicemanger.entity.PartitionsDevice;
import com.dykj.fireFighting.api.socialUnit.devicemanger.service.IPartitionsDeviceService;
import com.dykj.fireFighting.api.socialUnit.firemanger.entity.CoordinateRelation;
import com.dykj.fireFighting.api.socialUnit.firemanger.entity.FireStation;
import com.dykj.fireFighting.api.socialUnit.firemanger.entity.FireStationDevice;
import com.dykj.fireFighting.api.socialUnit.firemanger.service.ICoordinateRelationService;
import com.dykj.fireFighting.api.socialUnit.firemanger.service.IFireStationDeviceService;
import com.dykj.fireFighting.api.socialUnit.firemanger.service.IFireStationService;
import com.dykj.fireFighting.api.socialUnit.firemanger.vo.FireStationDeviceVo;
import com.dykj.fireFighting.api.socialUnit.xfxg.entity.FireGroupDevice;
import com.dykj.fireFighting.common.core.service.impl.BaseServiceImpl;
import com.dykj.fireFighting.common.core.utils.UUIDUtils;
import com.dykj.fireFighting.server.socialUnit.firemanger.mapper.FireStationMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.BeanUtils;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 消防管理-消防站
 *
 * @Author aixiaolan
 * @Date  2021-06-24 13:27:38
 */
@Slf4j
@DubboService
@Transactional(rollbackFor = Exception.class)
public class FireStationServiceImpl extends BaseServiceImpl<FireStationMapper, FireStation> implements IFireStationService {

    @Resource
    IFireStationDeviceService fireStationDeviceService;

    @Resource
    ICoordinateRelationService  coordinateRelationService;
    @Resource
    IPartitionsDeviceService partitionsDeviceService;

    @Transactional
    @Override
    public Boolean insertFireStation(FireStationDeviceVo fireStationDeviceVo){
        FireStation fireStation=new FireStation();
        BeanUtils.copyProperties(fireStationDeviceVo,fireStation);
        fireStation.setCreateTime(new Date());
        fireStation.setUpdateTime(new Date());
        String fireStationId=UUIDUtils.get32UUID();
        fireStation.setId(fireStationId);
        fireStation.setPartitionId(fireStationDeviceVo.getPartitionId());
        log.info("消防站写入数据：{}",fireStation.toString());
        save(fireStation);
        /*for(int i=0;i<fireStationDeviceVo.getDevice().size();i++){
           FireStationDevice fireStationDevice=new FireStationDevice();
            fireStationDevice.setDeviceId(fireStationDeviceVo.getDevice().get(i));
            fireStationDevice.setFireStationId(fireStationId);
            fireStationDevice.setType(0);
            fireStationDevice.setId(UUIDUtils.get32UUID());
            fireStationDeviceService.save(fireStationDevice);
        }*/
        for(int i=0;i<fireStationDeviceVo.getDeviceTradition().size();i++){
            FireStationDevice fireStationDevice=new FireStationDevice();
            fireStationDevice.setDeviceId(fireStationDeviceVo.getDeviceTradition().get(i));
            fireStationDevice.setFireStationId(fireStationId);
            fireStationDevice.setType(1);
            fireStationDevice.setId(UUIDUtils.get32UUID());
            fireStationDeviceService.save(fireStationDevice);
        }
        if(StringUtils.isNotBlank(fireStationDeviceVo.getPartitionId())){
            PartitionsDevice partitionsDevice=new PartitionsDevice();
            partitionsDevice.setId(UUIDUtils.get32UUID());
            partitionsDevice.setType(100);
            partitionsDevice.setDeviceId(fireStation.getId());
            partitionsDevice.setDeviceName(fireStation.getName());
            partitionsDevice.setY(fireStationDeviceVo.getLat());
            partitionsDevice.setX(fireStationDeviceVo.getLng());
            partitionsDevice.setPartitionId(fireStationDeviceVo.getPartitionId());
            log.info("插入分区坐标关系表：{}",partitionsDevice.toString());
            partitionsDeviceService.insertPoint(partitionsDevice);
        }else{
            //关系表
            CoordinateRelation coordinateRelation=new CoordinateRelation();
            coordinateRelation.setLng(fireStationDeviceVo.getLng());
            coordinateRelation.setLat(fireStationDeviceVo.getLat());
            coordinateRelation.setTableId(fireStationId);
            coordinateRelation.setId(UUIDUtils.get32UUID());
            coordinateRelation.setUnitId(fireStationDeviceVo.getUnitId());
            coordinateRelation.setTableType("0");
            log.info("插入坐标关系表：{}",coordinateRelation.toString());
            coordinateRelationService.save(coordinateRelation);
        }

        return true;
    }

    /**
     * 消防站表正常修改，消防站中间设备表先删除，再插入
     * @param fireStationDeviceVo
     * @return
     */
    @Transactional
    @Override
    public Boolean updateFireStation(FireStationDeviceVo fireStationDeviceVo){
        //修改
        FireStation fireStation=new FireStation();
        BeanUtils.copyProperties(fireStationDeviceVo,fireStation);
        fireStation.setUpdateTime(new Date());
        log.info("消防站修改数据：{}",fireStation.toString());
        updateById(fireStation);
        //删除中间设备表数据
        fireStationDeviceService.delFireStationById(fireStationDeviceVo.getId());
        //写入中间设备表的数据
        String fireStationId=fireStationDeviceVo.getId();
        /*for(int i=0;i<fireStationDeviceVo.getDevice().size();i++){
            FireStationDevice fireStationDevice=new FireStationDevice();
            fireStationDevice.setDeviceId(fireStationDeviceVo.getDevice().get(i));
            fireStationDevice.setFireStationId(fireStationId);
            fireStationDevice.setType(0);
            fireStationDevice.setId(UUIDUtils.get32UUID());
            fireStationDeviceService.save(fireStationDevice);
        }*/
        for(int j=0;j<fireStationDeviceVo.getDeviceTradition().size();j++){
            FireStationDevice fireStationDevice=new FireStationDevice();
            fireStationDevice.setDeviceId(fireStationDeviceVo.getDeviceTradition().get(j));
            fireStationDevice.setFireStationId(fireStationId);
            fireStationDevice.setType(1);
            fireStationDevice.setId(UUIDUtils.get32UUID());
            fireStationDeviceService.save(fireStationDevice);
        }
        //修改坐标关系表
        if(StringUtils.isNotBlank(fireStationDeviceVo.getPartitionId())){//判断坐标是否存在分区中
            //在分区
            PartitionsDevice partitionsDevice=new PartitionsDevice();
            partitionsDevice.setId(UUIDUtils.get32UUID());
            partitionsDevice.setType(100);
            partitionsDevice.setDeviceId(fireStation.getId());
            partitionsDevice.setDeviceName(fireStation.getName());
            partitionsDevice.setY(fireStationDeviceVo.getLat());
            partitionsDevice.setX(fireStationDeviceVo.getLng());
            partitionsDevice.setPartitionId(fireStationDeviceVo.getPartitionId());
            log.info("插入分区坐标关系表：{}",partitionsDevice.toString());
            int i = partitionsDeviceService.insertPoint(partitionsDevice);
            log.info("分区坐标关系表更新结果：{}",i);
            //删除园区中的坐标
            coordinateRelationService.deleteByTableId(fireStationId, "0");
        }else{//不在分区
            //判断是否存在坐标
            List<CoordinateRelation> byTableId = coordinateRelationService.getByTableId(fireStationId, "0");
            if(byTableId.size()>0){//存在
                //根据tableID更新坐标
                boolean result=coordinateRelationService.updateByTableID(fireStationDeviceVo.getLng(),fireStationDeviceVo.getLat(),
                        fireStationId);
                log.info("坐标关系表更新结果：{}",result);
            }else{//不存在插入坐标
                //关系表
                CoordinateRelation coordinateRelation=new CoordinateRelation();
                coordinateRelation.setLng(fireStationDeviceVo.getLng());
                coordinateRelation.setLat(fireStationDeviceVo.getLat());
                coordinateRelation.setTableId(fireStationId);
                coordinateRelation.setId(UUIDUtils.get32UUID());
                coordinateRelation.setUnitId(fireStationDeviceVo.getUnitId());
                coordinateRelation.setTableType("0");
                log.info("插入坐标关系表：{}",coordinateRelation.toString());
                coordinateRelationService.save(coordinateRelation);
            }
            //删除分区中的坐标
            partitionsDeviceService.deletePoint(fireStationDeviceVo.getId());
        }




        return true;


    }
    @Override
    public String getPictureByUnitId(String unitId){
        return baseMapper.getPictureByUnitId(unitId);
    }



}
